<?php
/**
 * 用户操作
 */
class ControllerUser extends ControllerBase
{
    /**
     * @var array $types 第三方账号类型
     */
    protected $types = array("weibo", "qq", "baidu");

    /**
     * 用户首页
     */
    public function actionIndex()
    {
        $uid = (int) G("uid");
        $cid = (int) G("cid");
        $kw = trim(G("kw", ""));
        $page = (int) G("p", 1);
        $each = 11;

        $user = M("User")->get($uid, "uid");
        if (!$user) {
            alert("用户不存在", Config::get("baseUrl"));
        }

        $categoryList = M("Category")->getListByUid($uid);

        $category = null;
        $cidsPublic = array();
        foreach ($categoryList as $v) {
            if (!$v["is_private"]) {
                $cidsPublic[] = $v["id"];
            }
            if ($v["id"] == $cid) {
                $category = $v;
            }
        }

        if ($cid) {
            if (is_null($category)) {
                alert("分类不存在", Router::buildUrl("User_Index", array("uid" => $uid)));
            }

            if ($uid != $this->mid && $category["is_private"]) {
                alert("无权查看此分类", Router::buildUrl("User_Index", array("uid" => $uid)));
            }
        }

        list($linkList, $count) = M("Link")->getList($page, $each, array(
            "uid" => $uid,
            "cid" => $cid ? $cid : ($uid == $this->mid ? null : $cidsPublic),
            "title" => strlen($kw) > 0 ? $kw : null,
            "mid" => $this->mid,
        ));

        Template::display("User_Index", array(
            "title" => $user["name"] . "的书签 - " . ($cid ? $category["name"] : (strlen($kw) > 0 ? "搜索 - " . $kw : "首页")),
            "mid" => $this->mid,
            "uid" => $uid,
            "user" => $user,
            "cid" => $cid,
            "kw" => $kw,
            "category" => $category,
            "categoryList" => $categoryList,
            "linkList" => $linkList,
            "pageHtml" => getPageHtml2(getPageUrl("p"), $page, $each, $count),
            "isPost" => session("link") ? true : false,
        ));
    }

    /**
     * 随机访问
     */
    public function actionRand()
    {
        $uid = M("Link")->getRandUidByCount(Config::get("randUserMinLinkCount"));
        if ($uid) {
            redirect(Router::buildUrl("User_Index", array("uid" => $uid)));
        } else {
            redirect(Config::get("baseUrl"));
        }
    }

    /**
     * 用户登录
     */
    public function actionLogin()
    {
        if (isPost()) {
            $account = P( "account" );
            $password = P( "password" );
            if (!$account || !$password) {
                returnJson( 0, "参数错误" );
            }
            $user = M( "User" )->getUserByAccount( $account );
            if (!$user) {
                returnJson( 0, "用户不存在" );
            }

            if ($user["password"] != md5( $password )) {
                returnJson( 0, "登陆失败" );
            }
            $uid = $user['uid'];
            $baseUrl = Config::get("baseUrl");
            session("mid", $uid);
            returnJson( 1, "登陆成功", ['baseUrl' => $baseUrl] );
        }
    }

    /**
     * 用户注册
     */
    public function actionRegister()
    {
        if (isPost()) {
            $account = P( "account" );
            $password = P( "password" );
            $confirm_password = P( "confirm_password" );
            if (!$account || !$password || !$confirm_password) {
                returnJson( 0, "参数错误" );
            }
            if ($password != $confirm_password) {
                returnJson( 0, "两次输入密码不一致" );
            }
            $user = M( "User" )->getUserByAccount( $account );
            if ($user) {
                returnJson( 0, "账号已存在，不能重复注册" );
            }
            $baseUrl = Config::get("baseUrl");
            $id = M("User")->add(array(
                'name'=>$account,
                'account'=>$account,
                'password'=>md5($account),
                'avatar'=>'http://bookmark.com/static/img/avatar.png'
            ));
            M("User")->set($id, array(
                "id" => $id,
                "url" => addslashes($baseUrl.'?do=User_Index&uid='.$id),
            ));
            $uid = $id;
            $baseUrl = Config::get("baseUrl");
            session("mid", $uid);
            returnJson( 1, "注册成功", ['baseUrl' => $baseUrl] );
        }
    }

    /**
     * 用户退出
     */
    public function actionLogout()
    {
        $app = G("app");
        if ($app == "mobile") {
            $baseUrl = BASE_URL . "/m/";
        } else {
            $baseUrl = Config::get("baseUrl");
        }

        session("mid", null);
        redirect($baseUrl);
    }
}
